﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//-------------------------------------------------------------------------------------

using System.Data;

namespace DotNet.Business
{
    using DotNet.BaseManager;
    using DotNet.DbUtilities;
    using DotNet.Model;
    using DotNet.Utilities;

    /// <summary>
    /// BaseRolePermissionScopeManager
    /// 角色权限域
    ///
    /// 修改纪录
    ///
    ///     2008.05.24 版本：1.0 DotNet 创建主键。
    ///
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2008.05.24</date>
    /// </author>
    /// </summary>
    public class BaseRolePermissionScopeManager : BaseManager, IBaseManager
    {
        public BaseRolePermissionScopeManager()
        {
            base.CurrentTableName = BasePermissionScopeTable.TableName;
        }

        public BaseRolePermissionScopeManager(IDbHelper dbHelper)
            : this()
        {
            DbHelper = dbHelper;
        }

        public BaseRolePermissionScopeManager(BaseUserInfo userInfo)
            : this()
        {
            UserInfo = userInfo;
        }

        public BaseRolePermissionScopeManager(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            UserInfo = userInfo;
        }

        #region public string[] GetOrganizeIds(string roleId, string permissionItemId) 获取角色的组织机构权限主键数组

        /// <summary>
        /// 获取角色的组织机构权限主键数组
        /// </summary>
        /// <param name="roleId">角色代吗</param>
        /// <param name="permissionItemId">权限代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetOrganizeIds(string roleId, string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BasePermissionScopeTable.FieldPermissionId;
            values[0] = permissionItemId;
            names[1] = BasePermissionScopeTable.FieldResourceCategory;
            values[1] = "Role";
            names[2] = BasePermissionScopeTable.FieldResourceId;
            values[2] = roleId;
            names[3] = BasePermissionScopeTable.FieldTargetCategory;
            values[3] = "Organize";
            DataTable dataTable = this.GetDataTable(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BasePermissionScopeTable.FieldTargetId);
            return returnValue;
        }

        #endregion public string[] GetOrganizeIds(string roleId, string permissionItemId) 获取角色的组织机构权限主键数组

        #region public string[] GetRoleIds(string roleId, string permissionItemId) 获取角色的角色权限主键数组

        /// <summary>
        /// 获取角色的角色权限主键数组
        /// </summary>
        /// <param name="roleId">角色代吗</param>
        /// <param name="permissionItemId">权限代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetRoleIds(string roleId, string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BasePermissionScopeTable.FieldPermissionId;
            values[0] = permissionItemId;
            names[1] = BasePermissionScopeTable.FieldResourceCategory;
            values[1] = "Role";
            names[2] = BasePermissionScopeTable.FieldResourceId;
            values[2] = roleId;
            names[3] = BasePermissionScopeTable.FieldTargetCategory;
            values[3] = "Role";
            DataTable dataTable = this.GetDataTable(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BasePermissionScopeTable.FieldTargetId);
            return returnValue;
        }

        #endregion public string[] GetRoleIds(string roleId, string permissionItemId) 获取角色的角色权限主键数组

        #region public string[] GetUserIds(string roleId, string permissionItemId) 获取角色的职员权限主键数组

        /// <summary>
        /// 获取角色的职员权限主键数组
        /// </summary>
        /// <param name="roleId">角色代吗</param>
        /// <param name="permissionItemId">权限代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetUserIds(string roleId, string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BasePermissionScopeTable.FieldPermissionId;
            values[0] = permissionItemId;
            names[1] = BasePermissionScopeTable.FieldResourceCategory;
            values[1] = "Role";
            names[2] = BasePermissionScopeTable.FieldResourceId;
            values[2] = roleId;
            names[3] = BasePermissionScopeTable.FieldTargetCategory;
            values[3] = "User";
            DataTable dataTable = this.GetDataTable(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BasePermissionScopeTable.FieldTargetId);
            return returnValue;
        }

        #endregion public string[] GetUserIds(string roleId, string permissionItemId) 获取角色的职员权限主键数组

        #region public string[] GetModuleIds(string resourceId, string permissionItemId) 获取资源的权限域主键数组

        /// <summary>
        /// 获取资源的权限域主键数组
        /// </summary>
        /// <param name="resourceId">资源代吗</param>
        /// <param name="permissionItemId">权限代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetModuleIds(string resourceId, string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BasePermissionScopeTable.FieldPermissionId;
            values[0] = permissionItemId;
            names[1] = BasePermissionScopeTable.FieldResourceCategory;
            values[1] = "Role";
            names[2] = BasePermissionScopeTable.FieldResourceId;
            values[2] = resourceId;
            names[3] = BasePermissionScopeTable.FieldTargetCategory;
            values[3] = "Module";
            DataTable dataTable = this.GetDataTable(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BasePermissionScopeTable.FieldTargetId);
            return returnValue;
        }

        #endregion public string[] GetModuleIds(string resourceId, string permissionItemId) 获取资源的权限域主键数组

        //
        // 授予权限域的实现部分
        //

        #region private string GrantOrganize(BasePermissionScopeManager permissionScopeManager, string id, string roleId, string permissionItemId, string organizeId) 为了提高授权的运行速度

        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="permissionScopeManager">权限域读写器</param>
        /// <param name="Id">主键</param>
        /// <param name="roleId">角色主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>主键</returns>
        private string GrantOrganize(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId, string organizeId)
        {
            string returnValue = string.Empty;
            BasePermissionScopeEntity resourcePermissionScope = new BasePermissionScopeEntity();
            resourcePermissionScope.PermissionId = int.Parse(permissionItemId);
            resourcePermissionScope.ResourceCategory = "Role";
            resourcePermissionScope.ResourceId = roleId;
            resourcePermissionScope.TargetCategory = "Organize";
            resourcePermissionScope.TargetId = organizeId;
            resourcePermissionScope.Enabled = 1;
            returnValue = permissionScopeManager.Add(resourcePermissionScope);
            return returnValue;
        }

        #endregion private string GrantOrganize(BasePermissionScopeManager permissionScopeManager, string id, string roleId, string permissionItemId, string organizeId) 为了提高授权的运行速度

        #region public string GrantOrganize(string roleId, string permissionItemId, string organizeId) 角色授予权限

        /// <summary>
        /// 角色授予权限
        /// </summary>
        /// <param name="roleId">角色主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>主键</returns>
        public string GrantOrganize(string roleId, string permissionItemId, string organizeId)
        {
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            return this.GrantOrganize(permissionScopeManager, roleId, permissionItemId, organizeId);
        }

        #endregion public string GrantOrganize(string roleId, string permissionItemId, string organizeId) 角色授予权限

        // nick
        public int GrantOrganize(string roleId, string permissionItemId, string[] organizeIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);

            string[] names = new string[5] { "ResourceId", "ResourceCategory", "TargetCategory", "PermissionId", "Enabled" };
            object[] values = new object[5] { roleId, "Role", "Organize", int.Parse(permissionItemId), 1 };
            string[] Ids = permissionScopeManager.GetIDs(names, values);
            if (Ids.Length > 0)
            {
                if (int.Parse(organizeIds[0]) != (int)PermissionScope.Min)
                {
                    // 删除该角色对应该权限的所有记录
                    for (int iIndex = 0; iIndex < Ids.Length; iIndex++)
                    {
                        permissionScopeManager.Delete(Ids[iIndex]);
                    }
                }
            }
            else
            {
                if (int.Parse(organizeIds[0]) == (int)PermissionScope.Min)
                {
                    organizeIds[0] = ((int)PermissionScope.None).ToString();
                }
            }
            for (int i = 0; i < organizeIds.Length; i++)
            {
                if (int.Parse(organizeIds[i]) != (int)PermissionScope.Min)
                {
                    this.GrantOrganize(permissionScopeManager, roleId, permissionItemId, organizeIds[i]);
                    returnValue++;
                }
            }
            return returnValue;
        }

        public int GrantOrganize(string[] roleIds, string permissionItemId, string organizeId)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                this.GrantOrganize(permissionScopeManager, roleIds[i], permissionItemId, organizeId);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantOrganize(string[] roleIds, string permissionItemId, string[] organizeIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                for (int j = 0; j < organizeIds.Length; j++)
                {
                    this.GrantOrganize(permissionScopeManager, roleIds[i], permissionItemId, organizeIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }

        //
        //  撤销权限域的实现部分
        //

        #region private int RevokeOrganize(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId, string organizeId) 为了提高撤销的运行速度

        /// <summary>
        /// 为了提高撤销的运行速度
        /// </summary>
        /// <param name="permissionScopeManager">权限域读写器</param>
        /// <param name="roleId">角色主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>影响行数</returns>
        private int RevokeOrganize(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId, string organizeId)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BasePermissionScopeTable.FieldResourceCategory;
            values[0] = "Role";
            names[1] = BasePermissionScopeTable.FieldResourceId;
            values[1] = roleId;
            names[2] = BasePermissionScopeTable.FieldTargetCategory;
            values[2] = "Organize";
            names[3] = BasePermissionScopeTable.FieldTargetId;
            values[3] = organizeId;
            return permissionScopeManager.BatchDelete(names, values);
        }

        #endregion private int RevokeOrganize(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId, string organizeId) 为了提高撤销的运行速度

        #region public int RevokeOrganize(string roleId, string permissionItemId, string organizeId) 撤销角色权限

        /// <summary>
        /// 撤销角色权限
        /// </summary>
        /// <param name="roleId">角色主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>影响行数</returns>
        public int RevokeOrganize(string roleId, string permissionItemId, string organizeId)
        {
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            return this.RevokeOrganize(permissionScopeManager, permissionItemId, roleId, organizeId);
        }

        #endregion public int RevokeOrganize(string roleId, string permissionItemId, string organizeId) 撤销角色权限

        public int RevokeOrganize(string roleId, string permissionItemId, string[] organizeIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < organizeIds.Length; i++)
            {
                returnValue += this.RevokeOrganize(permissionScopeManager, roleId, permissionItemId, organizeIds[i]);
            }
            return returnValue;
        }

        public int RevokeOrganize(string[] roleIds, string permissionItemId, string organizeId)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                returnValue += this.RevokeOrganize(permissionScopeManager, roleIds[i], permissionItemId, organizeId);
            }
            return returnValue;
        }

        public int RevokeOrganize(string[] roleIds, string permissionItemId, string[] organizeIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                for (int j = 0; j < organizeIds.Length; j++)
                {
                    returnValue += this.RevokeOrganize(permissionScopeManager, roleIds[i], permissionItemId, organizeIds[j]);
                }
            }
            return returnValue;
        }

        /////////////////////////////////////////////  Module 部分  /////////////////////////////////////////////////////

        //
        // 授予权限域的实现部分
        //

        #region private string GrantModule(BasePermissionScopeManager permissionScopeManager, string id, string roleId, string permissionItemId, string targetModuleId) 为了提高授权的运行速度

        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="permissionScopeManager">权限域读写器</param>
        /// <param name="Id">主键</param>
        /// <param name="roleId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetModuleId">职员主键</param>
        /// <returns>主键</returns>
        private string GrantModule(BasePermissionScopeManager permissionScopeManager, string id, string roleId, string permissionItemId, string targetModuleId)
        {
            string returnValue = string.Empty;
            BasePermissionScopeEntity resourcePermissionScope = new BasePermissionScopeEntity();
            resourcePermissionScope.PermissionId = int.Parse(permissionItemId);
            resourcePermissionScope.ResourceCategory = "Role";
            resourcePermissionScope.ResourceId = roleId;
            resourcePermissionScope.TargetCategory = "Module";
            resourcePermissionScope.TargetId = targetModuleId;
            resourcePermissionScope.Enabled = 1;
            return permissionScopeManager.Add(resourcePermissionScope);
        }

        #endregion private string GrantModule(BasePermissionScopeManager permissionScopeManager, string id, string roleId, string permissionItemId, string targetModuleId) 为了提高授权的运行速度

        #region public string GrantModule(string roleId, string permissionItemId, string targetModuleId) 职员授予权限

        /// <summary>
        /// 职员授予权限
        /// </summary>
        /// <param name="roleId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetModuleId">职员主键</param>
        /// <returns>主键</returns>
        public string GrantModule(string roleId, string permissionItemId, string targetModuleId)
        {
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            return this.GrantModule(permissionScopeManager, string.Empty, roleId, permissionItemId, targetModuleId);
        }

        #endregion public string GrantModule(string roleId, string permissionItemId, string targetModuleId) 职员授予权限

        public int GrantModules(string roleId, string permissionItemId, string[] targetModuleIds)
        {
            int returnValue = 0;
            BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);
            string[] sequenceIds = sequenceManager.GetBatchSequence(BasePermissionTable.TableName, targetModuleIds.Length);
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < targetModuleIds.Length; i++)
            {
                this.GrantModule(permissionScopeManager, sequenceIds[i], roleId, permissionItemId, targetModuleIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantModules(string[] roleIds, string permissionItemId, string targetModuleId)
        {
            int returnValue = 0;
            BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);
            string[] sequenceIds = sequenceManager.GetBatchSequence(BasePermissionTable.TableName, roleIds.Length);
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                this.GrantModule(permissionScopeManager, sequenceIds[i], roleIds[i], permissionItemId, targetModuleId);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantModule(string[] roleIds, string permissionItemId, string[] targetModuleIds)
        {
            int returnValue = 0;
            BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);
            string[] sequenceIds = sequenceManager.GetBatchSequence(BasePermissionTable.TableName, roleIds.Length * targetModuleIds.Length);
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                for (int j = 0; j < targetModuleIds.Length; j++)
                {
                    this.GrantModule(permissionScopeManager, sequenceIds[i * roleIds.Length + j], roleIds[i], permissionItemId, targetModuleIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }

        //
        //  撤销权限域的实现部分
        //

        #region private int RevokeModule(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId, string targetModuleId) 为了提高撤销的运行速度

        /// <summary>
        /// 为了提高撤销的运行速度
        /// </summary>
        /// <param name="permissionScopeManager">权限域读写器</param>
        /// <param name="roleId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetModuleId">职员主键</param>
        /// <returns>影响行数</returns>
        private int RevokeModule(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId, string targetModuleId)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BasePermissionScopeTable.FieldResourceCategory;
            values[0] = "Role";
            names[1] = BasePermissionScopeTable.FieldResourceId;
            values[1] = roleId;
            names[2] = BasePermissionScopeTable.FieldTargetCategory;
            values[2] = "Module";
            names[3] = BasePermissionScopeTable.FieldTargetId;
            values[3] = targetModuleId;
            return permissionScopeManager.BatchDelete(names, values);
        }

        #endregion private int RevokeModule(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId, string targetModuleId) 为了提高撤销的运行速度

        #region public int RevokeModule(string roleId, string permissionItemId, string targetModuleId) 撤销职员权限

        /// <summary>
        /// 撤销职员权限
        /// </summary>
        /// <param name="roleId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetModuleId">职员主键</param>
        /// <returns>影响行数</returns>
        public int RevokeModule(string roleId, string permissionItemId, string targetModuleId)
        {
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            return this.RevokeModule(permissionScopeManager, roleId, permissionItemId, targetModuleId);
        }

        #endregion public int RevokeModule(string roleId, string permissionItemId, string targetModuleId) 撤销职员权限

        public int RevokeModules(string roleId, string permissionItemId, string[] targetModuleIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < targetModuleIds.Length; i++)
            {
                returnValue += this.RevokeModule(permissionScopeManager, roleId, permissionItemId, targetModuleIds[i]);
            }
            return returnValue;
        }

        public int RevokeModule(string[] roleIds, string permissionItemId, string targetModuleId)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                returnValue += this.RevokeModule(permissionScopeManager, roleIds[i], permissionItemId, targetModuleId);
            }
            return returnValue;
        }

        public int RevokeModule(string[] roleIds, string permissionItemId, string[] targetModuleIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                for (int j = 0; j < targetModuleIds.Length; j++)
                {
                    returnValue += this.RevokeModule(permissionScopeManager, roleIds[i], permissionItemId, targetModuleIds[j]);
                }
            }
            return returnValue;
        }

        public int RevokeAll(string roleId)
        {
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);

            string[] names = new string[2];
            string[] values = new string[2];
            names[0] = BasePermissionScopeTable.FieldResourceCategory;
            values[0] = "Role";
            names[1] = BasePermissionScopeTable.FieldResourceId;
            values[1] = roleId;
            return permissionScopeManager.BatchDelete(names, values);
        }

        ////
        ////
        //// 授权范围管理部分
        ////
        ////

        #region public string[] GetLicensePermissionIds(string roleId) 获取职员的权限主键数组

        /// <summary>
        /// 获取职员的权限主键数组
        /// </summary>
        /// <param name="roleId">职员代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetLicensePermissionIds(string roleId)
        {
            string[] returnValue = null;
            string[] names = new string[3];
            string[] values = new string[3];
            names[0] = BasePermissionScopeTable.FieldResourceCategory;
            values[0] = "Role";
            names[1] = BasePermissionScopeTable.FieldResourceId;
            values[1] = roleId;
            names[2] = BasePermissionScopeTable.FieldTargetCategory;
            values[2] = "Permission";
            DataTable dataTable = this.GetDataTable(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BasePermissionScopeTable.FieldTargetId);
            return returnValue;
        }

        #endregion public string[] GetLicensePermissionIds(string roleId) 获取职员的权限主键数组

        //
        // 授予授权范围的实现部分
        //

        #region private string GrantLicense(BasePermissionScopeManager permissionScopeManager, string id, string roleId, string permissionItemId) 为了提高授权的运行速度

        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="permissionScopeManager">权限域读写器</param>
        /// <param name="Id">主键</param>
        /// <param name="roleId">职员主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <returns>主键</returns>
        private string GrantLicense(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId)
        {
            string returnValue = string.Empty;
            BasePermissionItemManager permissionItemManager = new BasePermissionItemManager(DbHelper);
            string licensePermissionId = permissionItemManager.GetID(BasePermissionItemTable.FieldCode, "LicensePermission");
            BasePermissionScopeEntity resourcePermissionScopeEntity = new BasePermissionScopeEntity();
            resourcePermissionScopeEntity.PermissionId = int.Parse(licensePermissionId);
            resourcePermissionScopeEntity.ResourceCategory = "Role";
            resourcePermissionScopeEntity.ResourceId = roleId;
            resourcePermissionScopeEntity.TargetCategory = "Permission";
            resourcePermissionScopeEntity.TargetId = permissionItemId;
            resourcePermissionScopeEntity.Enabled = 1;
            return permissionScopeManager.Add(resourcePermissionScopeEntity);
        }

        #endregion private string GrantLicense(BasePermissionScopeManager permissionScopeManager, string id, string roleId, string permissionItemId) 为了提高授权的运行速度

        #region public string GrantLicense(string roleId, string permissionItemId) 职员授予权限

        /// <summary>
        /// 职员授予权限
        /// </summary>
        /// <param name="roleId">职员主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>主键</returns>
        public string GrantLicense(string roleId, string permissionItemId)
        {
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            return this.GrantLicense(permissionScopeManager, roleId, permissionItemId);
        }

        #endregion public string GrantLicense(string roleId, string permissionItemId) 职员授予权限

        public int GrantLicense(string roleId, string[] permissionItemIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < permissionItemIds.Length; i++)
            {
                this.GrantLicense(permissionScopeManager, roleId, permissionItemIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantLicense(string[] roleIds, string permissionItemId)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                this.GrantLicense(permissionScopeManager, roleIds[i], permissionItemId);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantLicense(string[] roleIds, string[] permissionItemIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                for (int j = 0; j < permissionItemIds.Length; j++)
                {
                    this.GrantLicense(permissionScopeManager, roleIds[i], permissionItemIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }

        //
        //  撤销授权范围的实现部分
        //

        #region private int GrantLicense(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId) 为了提高授权的运行速度

        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="permissionScopeManager">权限域读写器</param>
        /// <param name="roleId">职员主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <returns>主键</returns>
        private int RevokeLicense(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BasePermissionScopeTable.FieldResourceCategory;
            values[0] = "Role";
            names[1] = BasePermissionScopeTable.FieldResourceId;
            values[1] = roleId;
            names[2] = BasePermissionScopeTable.FieldTargetCategory;
            values[2] = "Permission";
            names[3] = BasePermissionScopeTable.FieldTargetId;
            values[3] = permissionItemId;
            return permissionScopeManager.BatchDelete(names, values);
        }

        #endregion private int GrantLicense(BasePermissionScopeManager permissionScopeManager, string roleId, string permissionItemId) 为了提高授权的运行速度

        #region public int GrantLicense(string roleId, string permissionItemId) 职员撤销授权

        /// <summary>
        /// 职员撤销授权
        /// </summary>
        /// <param name="roleId">职员主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <returns>主键</returns>
        public int RevokeLicense(string roleId, string permissionItemId)
        {
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            return this.RevokeLicense(permissionScopeManager, roleId, permissionItemId);
        }

        #endregion public int GrantLicense(string roleId, string permissionItemId) 职员撤销授权

        public int RevokeLicense(string roleId, string[] permissionItemIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < permissionItemIds.Length; i++)
            {
                this.RevokeLicense(permissionScopeManager, roleId, permissionItemIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int RevokeLicense(string[] roleIds, string permissionItemId)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                this.RevokeLicense(permissionScopeManager, roleIds[i], permissionItemId);
                returnValue++;
            }
            return returnValue;
        }

        public int RevokeLicense(string[] roleIds, string[] permissionItemIds)
        {
            int returnValue = 0;
            BasePermissionScopeManager permissionScopeManager = new BasePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                for (int j = 0; j < permissionItemIds.Length; j++)
                {
                    this.RevokeLicense(permissionScopeManager, roleIds[i], permissionItemIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }
    }
}